package com.baoming.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.ISingupEnterDao;
import com.baoming.domain.SingupEnter;
import com.baoming.service.ISingupEnterService;
import com.baoming.util.PageInfo;

@Transactional(readOnly=true)
@Service(ISingupEnterService.SERVICE)
public class SingupEnterServiceImpl implements ISingupEnterService {

	@Resource(name=ISingupEnterDao.SERVICE)
	private ISingupEnterDao singupDao ; 
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveSingupEnter(SingupEnter enter) {
		singupDao.save(enter) ; 
	}

	public List<SingupEnter> selectSingupEnterBySingupId(String singupId , PageInfo pageInfo) {
		String hql = " and m.singupId = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(singupId) ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<SingupEnter> enters = singupDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return enters ;
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteSingupEnter(String id) {
		singupDao.deleteObjectById(id) ; 
	}

	/*
	 * 查询出多个比赛的报名
	 * @see com.baoming.service.ISingupEnterService#selectSingupEnterBySingupId(java.lang.String)
	 */
	public List<SingupEnter> selectSingupEnterBySingupIds(String ids , PageInfo pageInfo) {
		String hql = " and m.singupId in ("+ids+") " ;
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<SingupEnter> enters = singupDao.findCollectionByConditionWithPage(hql, null, orderby, pageInfo) ; 
		return enters ;
	}

	public int selectCountEnter(String id) {
		return singupDao.findCountEnter(id);
	}

	//查询出一个比赛的所有报名人数
	public List<SingupEnter> selectSingupEnterBySingupId(String id) {
		String hql = " and m.singupId = ? " ;
		List<String> params = new ArrayList<String>() ; 
		params.add(id) ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		return singupDao.findCollectionByConditionNoPage(hql, params.toArray(), orderby);
	}
  
	public List<SingupEnter> selectReSingupEnterBySingupIds(String ids) {
		List<SingupEnter> enters = singupDao.findCollectionByConditionRect(ids) ; 
		return enters ; 
	}

	public SingupEnter selectEnter(String enterId) {
		return singupDao.findObjectById(enterId);
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void tagEnter(String id , int tag ) {
		singupDao.tagEnter(id ,tag );
	}

	public SingupEnter selectEnterById(String id) {
		return singupDao.findObjectById(id) ;
	}

	/**
	 * 分页搜索查询 
	 */
	public List<SingupEnter> selectSingupEnterBySingupId(String keyword,
			String singupId, PageInfo pageInfo) {
		String hql = " and m.singupId = ? and m.xmlValue like ? " ;
		List<String> params = new ArrayList<String>() ; 
		params.add(singupId) ; 
		params.add("%"+keyword+"%") ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<SingupEnter> enters = singupDao.findCollectionByConditionWithPage(hql, params.toArray() , orderby, pageInfo) ; 
		return enters ; 
	}
  
}
